package com.peppa.sirius.esaml.design;

import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.viewpoint.DSemanticDecorator;
import org.eclipse.xtext.EcoreUtil2;

import com.peppa.xtext.esaml.esaml.ActivityImpl;
import com.peppa.xtext.esaml.esaml.ActivityInteractions;
import com.peppa.xtext.esaml.esaml.ActivitySubComponent;
import com.peppa.xtext.esaml.esaml.StateMachine;
import com.peppa.xtext.esaml.esaml.SubComponent;
import com.peppa.xtext.esaml.esaml.SubSystemImpl;
import com.peppa.xtext.esaml.esaml.SystemImpl;
import com.peppa.xtext.esaml.esaml.impl.ActivityImplImpl;
import com.peppa.xtext.esaml.esaml.impl.ActivityInteractionsImpl;
import com.peppa.xtext.esaml.esaml.impl.PortFeatureImpl;
import com.peppa.xtext.esaml.esaml.impl.ScheduleImpl;
import com.peppa.xtext.esaml.esaml.impl.SubComponentImpl;
import com.peppa.xtext.esaml.esaml.impl.SubSystemImplImpl;
import com.peppa.xtext.esaml.esaml.impl.SystemImplImpl;

public class SectionToolsServices{
	
	/**
	 * 创建PortChannel或AccessChannel时，获取PortChannel或AccessChannel所属的SystemImpl
	 * @param source
	 * @param sourceView
	 * @return
	 */
	public SystemImpl getPortOrAccessChannelContainer_SystemImpl(EObject source, DSemanticDecorator sourceView) {
		EObject rootElement = EcoreUtil2.getRootContainer(source);
        List<SystemImpl> candidates = EcoreUtil2.getAllContentsOfType(rootElement, SystemImpl.class);
//		System.out.println(candidates);
		for(SystemImpl systemImpl : candidates) {
			SystemImplImpl systemImplImpl = (SystemImplImpl) systemImpl;
			if(systemImplImpl.getName().equals(sourceView.eContainer().eContainer().toString()) || systemImplImpl.getName().equals(sourceView.eContainer().toString())) {
				return systemImpl;
			}
		}
//		System.out.println("getPortChannelContainer_SystemImpl");
		return null;
	}
	
	/**
	 * 创建PortChannel或AccessChannel时，获取PortChannel或AccessChannel所属的SubSystemImpl下的ActivityInteractions
	 * @param source
	 * @param sourceView
	 * @return
	 */
	public ActivityInteractions getPortOrAccessChannelContainer_SubSsytemImpl_ActivityInteractions(EObject source, DSemanticDecorator sourceView) {
		EObject rootElement = EcoreUtil2.getRootContainer(source);
        List<SubSystemImpl> candidates = EcoreUtil2.getAllContentsOfType(rootElement, SubSystemImpl.class);
//		System.out.println(candidates);
		for(SubSystemImpl subSystemImpl : candidates) {
			SubSystemImplImpl subSystemImplImpl = (SubSystemImplImpl) subSystemImpl;
			if(subSystemImplImpl.getName().equals(sourceView.eContainer().eContainer().toString()) || subSystemImplImpl.getName().equals(sourceView.eContainer().toString())) {
//				System.out.println(sourceView.eContainer().toString());
//				System.out.println(sourceView.eContainer().eContainer().toString());
				return subSystemImplImpl.getActivityInteractions();
			}
		}
		System.out.println("getPortChannelContainer_SystemImpl");
		return null;
	}
	
	/**
	 * 创建PortChannel时，获取PortChannel的端口类型
	 * @param source
	 * @param target
	 * @return
	 */
	public String getPortChannelType(EObject source, EObject target) {
		PortFeatureImpl sourcePortFeatureImpl = (PortFeatureImpl) source;
		PortFeatureImpl targetPortFeatureImpl = (PortFeatureImpl) target;
		if(sourcePortFeatureImpl.getType().equals(targetPortFeatureImpl.getType())) {
			return sourcePortFeatureImpl.getType();
		}else {
			System.out.println("sourcePort与targetPort类型不一致(sourcePort:"+sourcePortFeatureImpl.getType()+",targetPort:"+targetPortFeatureImpl.getType()+")");
			return null;
		}
	}
	
	/**
	 * 创建PortChannel时，获取PortChannel的SourceSubComponent或SinkSubComponent
	 * 创建AccessChannel时，获取AccessChannel的SourceSubComponent
	 * @param self
	 * @param sourceView
	 * @return
	 */
	public SubComponent getPortOrAccessChannelSourceOrSinkSubComponent(EObject source , DSemanticDecorator sourceOrTargetView, String implType) {
//		System.out.println("进入getPortChannelSourceOrSinkSubComponent");
		List<SubComponent> candidates = null;
		if("SystemImpl".equals(implType)) {
			SystemImplImpl systemImplImpl = (SystemImplImpl) getPortOrAccessChannelContainer_SystemImpl(source, sourceOrTargetView);
			candidates = EcoreUtil2.getAllContentsOfType(systemImplImpl, SubComponent.class);
		}else if("SubSystemImpl".equals(implType)) {
			ActivityInteractionsImpl activityInteractionsImpl = (ActivityInteractionsImpl) getPortOrAccessChannelContainer_SubSsytemImpl_ActivityInteractions(source, sourceOrTargetView);
			candidates = EcoreUtil2.getAllContentsOfType(activityInteractionsImpl, SubComponent.class);
		}else {
			System.out.println("获取PortChannel的SourceSubComponent或SinkSubComponent时出现错误");
			return null;
		}
		
//		System.out.println(candidates);
//		System.out.println(sourceOrTargetView.eContainer().toString().split(" ")[1]);
		for(SubComponent subComponent : candidates) {
			SubComponentImpl subComponentImpl = (SubComponentImpl) subComponent;
			if(subComponentImpl.getName().equals(sourceOrTargetView.eContainer().toString().split(" ")[1])) {
//				System.out.println(subComponent);
				return subComponent;
			}
		}
		System.out.println("创建selfPortChannel或selfAccessChannel");
		return null;
	}
	
	/**
	 * 删除某个控制连接时，将该连接源节点之后的所有节点从控制流中清除出去
	 * 当删除的是第一个控制连接时，将全部控制流清除
	 * @param self
	 * @return
	 */
	public String deleteControlFlow(EObject self) {
		System.out.println(self.eContainer());
		ActivityInteractionsImpl activityInteractionsImpl = (ActivityInteractionsImpl) self.eContainer();
		ScheduleImpl scheduleImpl = (ScheduleImpl) activityInteractionsImpl.getActivityControlFlow();
		EList<ActivitySubComponent> activitySubComponents = scheduleImpl.getActivitys();
		int i = activitySubComponents.size() - 1;
		while(self != activitySubComponents.get(i)) {
			activitySubComponents.remove(i);
			i--;
		}
		if(i==0) {
			activitySubComponents.remove(i);
			activityInteractionsImpl.setActivityControlFlow(null);
		}
//		
//		System.out.println("getPortChannelContainer_SystemImpl");
		return null;
	}
	
	/**
	 * 创建状态机的Transitions时，获取Transition所属的ActivityImpl下的StateMachine
	 * @param source
	 * @param sourceView
	 * @return
	 */
	public StateMachine getTransitionContainer_ActivityImpl_StateMachine(EObject source, DSemanticDecorator sourceView) {
		EObject rootElement = EcoreUtil2.getRootContainer(source);
        List<ActivityImpl> candidates = EcoreUtil2.getAllContentsOfType(rootElement, ActivityImpl.class);
		for(ActivityImpl activityImpl : candidates) {
			ActivityImplImpl activityImplImpl = (ActivityImplImpl) activityImpl;
			if(activityImplImpl.getName().equals(sourceView.eContainer().toString())) {
//				System.out.println("进入获得状态机"+activityImplImpl.getStateMachine());
				return activityImplImpl.getStateMachine();
			}
		}
		return null;
	}

}